package com.lylg.picture_share.controller;

import com.github.pagehelper.PageInfo;
import com.lylg.picture_share.entity.Manager;
import com.lylg.picture_share.service.ManagerService;
import org.omg.CORBA.StringHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager")

public class ManagerController {
    private PageInfo<Manager> pageInfo;
    @Autowired
    private ManagerService managerService;
    @RequestMapping("/loginui")
    public String loginUi(){

        return "manager/managerlogin";
    }


    @RequestMapping("/exit")
    public String doExit(HttpSession session){
        //从session中移除
        session.removeAttribute("loginmanager");
        return "redirect:/manager/loginui";

    }

//   @RequestMapping("/login")
//    public String login(String manager_name, String manager_pwd , HttpSession session){
//        Manager manager =managerService.selectByNameAndPwd(manager_name,manager_pwd);
//        if(manager!=null){
//
////            session.setAttribute("loginmanager", manager);
//            //登录成功，去主界面
//            return "redirect:/manager/main";
//        }
//        else{
//            //登录失败，去登录界面（重定向）
//            return "redirect:/manager/loginui";
//        }
//    }
     @RequestMapping("/login")
    public String login(String manager_name, String manager_pwd , HttpSession session){
        Manager manager =managerService.selectByNameAndPwd(manager_name,manager_pwd);
        if(manager!=null){

           session.setAttribute("loginmanager", manager);
            //登录成功，去主界面
            return "redirect:/manager/toindex";
        }
        else{
            //登录失败，去登录界面（重定向）
            return "redirect:/manager/loginui";
        }
    }
    @RequestMapping("/tableui")
    public String indexui(Map<String,Object> model){
      List<Manager> l = managerService.selectManagerAll();

        model.put("managers",l);


        return "/manager/manager";
    }
    @GetMapping("/del/{manager_id}")
    public String deletemanager(@PathVariable("manager_id") int id){
        managerService.deleteManager(id);
        return "redirect:/manager/tableui";
    }

    public Manager selectByName(String name){
        Manager m = managerService.selectByName(name);
        return m;
    }

    @RequestMapping("/toinsert")
    public String toInsertManager(){
        return "/manager/addmanager";
    }
    @PostMapping("/insert")
    public String insertManager(Manager manager){
       managerService.insertManager(manager);
       return "redirect:/manager/tableui";
    }
@RequestMapping("/toupdpwd")
public String toUpdPwd(){
        return "manager/updpwd";
}

   @RequestMapping("/tomodal")
    public String modal(){
        return "manager/modal.html";
    }

    @RequestMapping("/toimage")
    public String toImage(){
        return "/manager/image";
    }
    @RequestMapping("/toimagetype")
    public String toImageType(){
        return "/manager/imagetype";
    }

    @RequestMapping("/toindex")
    public String toIndex(){
        return "/manager/index";
    }

    @RequestMapping("/toselect")
    public String toSelect(){
        return "/manager/select";
    }



    //manager：会将查询条件按照名称一致的原则（表单控件的名称和实体类属性的名称）进行自动封装
    //currentPage参数名称要和url传值的参数名称保持一致
    @RequestMapping("/listui")
    public String doList(Map<String,Object> model, Manager queryBean, Integer currentPage){

        //因为页面首次加载的时候传递不过来页码信息
        if(currentPage==null){

            currentPage=1;
        }
        else{
            //防止点击上一页越界
            if(currentPage==0){
                currentPage=1;
            }else{

                if(currentPage>pageInfo.getPages()){
                    currentPage=pageInfo.getPages();
                }
                //防止点击上一页越界
                if(currentPage==0){
                    currentPage=1;
                }
            }

        }

        Integer pageSize=4;
        //根据查询条件查询数据
        pageInfo=managerService.getManagers(queryBean,currentPage,pageSize);


        //pageInfo类型对象中封装了分页相关的所有信息（getList）
        //有一个是什么样的方法可以得到查询到的第一页的数据（List）
        //将数据放到模型中
        model.put("qb",queryBean);//将查询条件也放到数据模型中
        //将pageInfo整个对象转过去
        model.put("pb",pageInfo);

        //会去thymeleaf模板所处的位置找对应的文件
        return "/manager/manager";

    }

    @RequestMapping("/updpwd")
    public String doUpdPwd(String manager_pwd,HttpSession session){

        Integer managerId=0;
        if(session.getAttribute("loginmanager")!=null){
            managerId=((Manager)session.getAttribute("loginmanager")).getManager_id();
        }

        //修改密码
        managerService.updPwd(managerId,manager_pwd);

        //清除当前的登录状态，让用户重新登录
        session.removeAttribute("loginmanager");
        return "redirect:/manager/loginui";

    }

    @RequestMapping("/updstatus")
    public String doUpdStatus(Integer manager_id){

        managerService.updateStatus(manager_id);

        return "redirect:/manager/listui";

    }




    //manager中只封装了名称和角色
    @RequestMapping("/save")
    public String doSave(Manager manager){
        manager.setManager_status("正常");
        manager.setManager_pwd("123456");
        managerService.saveManager(manager);
        return "redirect:/manager/listui";
    }
    @RequestMapping("/restpwd")
    public String doRestPwd(String manager_pwd,HttpSession session){

        Integer managerId=0;
        if(session.getAttribute("loginmanager")!=null){
            managerId=((Manager)session.getAttribute("loginmanager")).getManager_id();
        }
        //初始化密码
        manager_pwd="123456";

        //重置密码
        managerService.updPwd(managerId,manager_pwd);

        //清除当前的登录状态，让用户重新登录
        session.removeAttribute("loginmanager");
        return "redirect:/manager/loginui";


    }

}
